import org.apache.tools.ant.util.FileUtils;

import net.sf.jasperreports.engine.JasperReport
import net.sf.jasperreports.engine.JRExporter
import net.sf.jasperreports.engine.JRExporterParameter
import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperPrint
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import net.sf.jasperreports.engine.export.JRCsvExporter
import net.sf.jasperreports.engine.export.JRHtmlExporter
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter
import net.sf.jasperreports.engine.export.JRPdfExporter
import net.sf.jasperreports.engine.export.JRRtfExporter
import net.sf.jasperreports.engine.export.JRTextExporter
import net.sf.jasperreports.engine.export.JRTextExporterParameter
import net.sf.jasperreports.engine.export.JRXlsExporter
import net.sf.jasperreports.engine.export.JRXlsExporterParameter
import net.sf.jasperreports.engine.export.JRXmlExporter
import net.sf.jasperreports.engine.util.JRLoader
import net.sf.jasperreports.engine.JasperRunManager

class JasperViewController {
	WhereClauseService whereClauseService	
	javax.sql.DataSource dataSource
	
	def index = {
		if(session.reportId){
			def conn = dataSource.getConnection()
			byte[] bytes =null
			def paramMap = session.paramMap
			Integer systemId = new Integer(session.systemId)
			Integer reportId = new Integer(session.reportId)
			def rpt = Report.get(reportId)
			def rptSystem = ReportSystem.get(systemId)
			
			def whereClause = whereClauseService.generateWhereClause(paramMap,systemId)
		
			def nonFilterableParam = ReportParameter.withCriteria{
				eq('parameterFilterable','N')
				eq('report.id',reportId)
				eq('parameterEnded','N')
			}
			
			HashMap parameters = whereClauseService.assignParameter(nonFilterableParam)
			parameters.put("SUBREPORT_DIR", rptSystem.reportDirectory)
			parameters.put("REPORT_LOCALE", request.getLocale())
			if(whereClause)
				parameters.put("pWhereClause", whereClause)
			if(rpt.orderByClauseOverride)
				parameters.put("pOrderByClause", rpt.orderByClauseOverride)
			parameters.put("pTitle", rpt.reportTitle)	
		
			try{
				File sourceFile = new File(rptSystem.reportDirectory + rpt.reportName)
				if(sourceFile.canRead()){
					JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile)
					bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, conn)
				}
				else{
					throw new Exception("Unknown Jasper Report File: " + sourceFile)
				}
			}
			finally{
				conn.close()
			}
		
			response.setHeader("Content-Disposition","inline; filename=" + rpt.reportName.replace(".jasper", ".pdf"))
			response.contentType = "application/pdf"
			response.outputStream << bytes
		}
		else{
			response.setHeader("Content-Disposition","inline")
			response.contentType = "text/plain"
			response.outputStream << "Can not generate a report: "+
									 "No Report selected or your report "+
									 "selection has expired."
		}
	}
}